package com.robot.mvvm.ui.work

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.util.LiveDataBus
import com.robot.base.util.OnSoftKeyBoardChangeListener
import com.robot.base.util.SoftKeyBoardListener
import com.robot.base.util.arouter.ARouterUtil
import com.robot.mvvm.BR
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppActivitySafetyBinding
import com.robot.mvvm.ui.work.adapter.SafetyAdapter
import com.robot.mvvm.vm.work.SafetyViewModel

@Route(path = AppConstants.Router.Main.A_SAFETY)
class SafetyActivity : GLBaseActivity<AppActivitySafetyBinding, SafetyViewModel>() {

    var mSafetyAdapter: SafetyAdapter? = null
    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_safety

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        addSingleTitleBar(getString(R.string.app_tips_49), getString(R.string.app_tips_78))
        initAdapter()
        viewModel.refresh()
    }

    fun initAdapter() {
        mSafetyAdapter = SafetyAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_SAFETY_DETAIL,
                    Bundle().apply
                    {
                        putString(
                            AppConstants.BundleKey.PERSON_ID,
                            mSafetyAdapter?.data?.get(position)?.personId
                        )
                    })
            }
        }
        binding.mRv.apply {
            layoutManager = LinearLayoutManager(this@SafetyActivity)
            adapter = mSafetyAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.getUC().onRightEvent.observe(this) {
            ARouterUtil.goToActivity(AppConstants.Router.Main.A_ADD_SAFETY)
        }
        viewModel.uc.refreshEvent.observe(this, Observer {
            it ?: return@Observer
            binding.refreshLayout.finishRefresh()
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.setEnableLoadMore(it != 1)
        })
        viewModel.uc.refreshDataEvent.observe(this, Observer {
            if (viewModel.pageNo == 1) {
                mSafetyAdapter?.data?.clear()
            }
            mSafetyAdapter?.addData(it)
        })
        binding.refreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            viewModel.loadNext()
        }
        LiveDataBus.get().with(AppConstants.LiveKey.SAFETY_REFRESH_DATA).observe(this) {
            viewModel.refresh()
        }

        SoftKeyBoardListener.setListener(this, object : OnSoftKeyBoardChangeListener {
            override fun keyBoardShow() {
            }

            override fun keyBoardHide() {
                viewModel.refresh()
            }
        })
    }
}